﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AWSV2.Services
{
    public static class ProtocolParseHelper
    {
        public static string ProtocolParse(string protrcolType, byte[] byteBuf)
        {
            string weightString;
            decimal weightNum;
            int decimalPoint;
            string ret = string.Empty;

            switch (protrcolType)
            {
                case "耀华": //02(2B 30 30 30 30 30 30 32 31 3B 03)

                    weightString = Encoding.ASCII.GetString(byteBuf, 0, 7);
                    try
                    {
                        weightNum = Convert.ToDecimal(weightString);

                        decimalPoint = byteBuf[7] - 0x30;

                        while (decimalPoint-- > 0)
                        {
                            weightNum /= 10;
                        }

                        ret = weightNum.ToString();
                    }
                    catch { }
                    break;

                case "昌信": //FF(11 00 00 00)

                    weightNum = (byteBuf[3] / 16 * 10 + byteBuf[3] % 16) * 10000
                        + (byteBuf[2] / 16 * 10 + byteBuf[2] % 16) * 100
                        + (byteBuf[1] / 16 * 10 + byteBuf[1] % 16);

                    decimalPoint = (byteBuf[0] & 0x0F) - 1;
                    while (decimalPoint-- > 0)
                    {
                        weightNum /= 10;
                    }

                    if ((byteBuf[0] & 0x20) > 0)
                    {
                        weightNum = 0 - weightNum;
                    }

                    ret = weightNum.ToString();

                    break;

                case "金钟":  //ST,GS,+00000.0kg\r\nST,GS,+00000.0kg\r\n

                    weightString = Encoding.ASCII.GetString(byteBuf, 6, 8);
                    try
                    {
                        weightNum = Convert.ToDecimal(weightString);

                        ret = weightNum.ToString();
                    }
                    catch { }

                    break;

                case "衡天":  //(02 20 30 20 30 30 30 30 30 30 0D)0A

                    weightString = Encoding.ASCII.GetString(byteBuf, 4, 6);
                    try
                    {
                        weightNum = Convert.ToDecimal(weightString);

                        decimalPoint = (byteBuf[1] & 0x07) - 2;
                        while (decimalPoint-- > 0)
                        {
                            weightNum /= 10;
                        }

                        if ((byteBuf[2] & 0x02) > 0)
                        {
                            weightNum = 0 - weightNum;
                        }

                        ret = weightNum.ToString();
                    }
                    catch { }

                    break;

                case "托利多":  //02(3A 30 20 20 20 20 20 20 30)20 20 20 20 20 30 0D C7 

                    weightString = Encoding.ASCII.GetString(byteBuf, 3, 6);

                    try
                    {
                        weightNum = Convert.ToDecimal(weightString);

                        decimalPoint = (byteBuf[0] & 0x07) - 2;
                        while (decimalPoint-- > 0)
                        {
                            weightNum /= 10;
                        }

                        if ((byteBuf[1] & 0x02) > 0)
                        {
                            weightNum = 0 - weightNum;
                        }

                        ret = weightNum.ToString();
                    }
                    catch { }

                    break;
                    
                case "A27":  //=84.1200
                    byte[] byteNew = new byte[7];
                    for (int i = 0; i < 7; i++)
                    {
                        byteNew[6 - i] = byteBuf[i];
                    }
                    weightString = Encoding.ASCII.GetString(byteNew, 0, 7);

                    try
                    {
                        weightNum = Convert.ToDecimal(weightString);

                        if (weightNum == 0) weightNum = 0;

                        ret = weightNum.ToString();
                    }
                    catch { }

                    break;

                case "A12":  //77(6E 30 30 31 39 2E 38 36 6B 67 0D 0A)
                    weightString = Encoding.ASCII.GetString(byteBuf, 1, 7);
                    try
                    {
                        weightNum = Convert.ToDecimal(weightString);

                        ret = weightNum.ToString();
                    }
                    catch { }
                    break;

                case "西班牙":  
                    //01 02 30 34 30 32 30 30
                    //02 30 31 30 30 30 30 31 30 2E 6B 67 20
                    //02 30 32 30 30 30 30 30 30 2E 6B 67 20
                    //02 30 33 30 30 30 30 31 30 2E 6B 67 20
                    //0D 0A 
                    
                    weightString = Encoding.ASCII.GetString(byteBuf, 11, 6);
                    try
                    {
                        weightNum = Convert.ToDecimal(weightString);
                                                
                        ret = weightNum.ToString();
                    }
                    catch { }
                    break;

                case "四方":
                    // CD 0A 0A 01 06 08 00 CD 0A 0B 01 07 02 00 

                    try
                    {
                        var strArray = new List<string>();
                        byteBuf = byteBuf.Take(6).ToArray();
                        foreach (var item in byteBuf)
                        {
                            if (item == 0x0A)
                            {
                                continue;
                            }
                            if (item == 0x0B && !strArray.Any(p=>p == "-"))
                            {
                                strArray.Add("-");
                                continue;
                            }
                            strArray.Add(Convert.ToInt32(item).ToString());
                        }
                        
                        ret = string.Join("", strArray);
                    }
                    catch { }
                    break;

                default:
                    break;
            }

            return ret;
        }
    }
}
